package ru.niir.goodfil.ui.components;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class TimestampConverter implements Converter
{
    private static SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");

    public TimestampConverter()
    {
    }

    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s)
    {
        try
        {
            return new Timestamp(sdf.parse(s).getTime());
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o)
    {
        try
        {
            return sdf.format(o);
        }
        catch (Exception e)
        {
            return "???";
        }
    }
}
